<?php
class View_Admin_CategoryManangement extends View_Admin_Index_Template {
    public $oTemp;
    public function displayManagerIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN . 'html/category_management_index.html');
        $this->oTemp->assign('pageHeding', $oModel->aLanguage[MODULE_HEADING]['category']);
        /**
         * toolbar setup
         */
        $this->oTemp->assign('pathway_file', $oModel->getData('cate_dir') );
        $this->oTemp->assign('toobar_new', '?sMod=category_index&sAction=add_new_form&parent_category_id='.$oModel->getData('parent_category_id').'&parent_category_name='.$oModel->getData('parent_category_name').'&cate_dir='.urlencode($oModel->getData('cate_dir')));
        $this->oTemp->parse('main.toobar_new');
        $this->oTemp->assign('toobar_save',"$('frmUpdateCate').submit();");
        $this->oTemp->parse('main.toobar_save');
    }

    public function displayListCategoryNew($oModel) {
        $rs = $oModel->getListParentCategory();
        if (is_array($rs)) {
            $i = 0;
            foreach ($rs as $row) {
                $i++;
                $row['no'] = $i;
                $row['css'] = "row" . ($i % 2);
                $row['prefix'] = $this->displayTreePrefixImg(1);
                if ($row['product_category_status'] == 1) {
                    $row['product_category_status'] = 'checked';
                }

                if ($row['product_category_statistics'] == 1) {
                    $row['product_category_statistics'] = 'checked';
                }

                if ($_SESSION['category_change_igm_id'] != '' && $_SESSION['category_change_igm_id'] == $row['product_category_id']) {
                    $category_image_old = $row['product_category_image'];
                    $oModel->deleteThumbImage($category_image_old);
                }
                $row['product_category_image_thumb'] = $oModel->getThumbImage($row['product_category_image'], 20, 20);
                $row['product_category_image_thumb'] = '<img src="'.$row['product_category_image_thumb'].'" border="0" alt="'.$row['product_category_name'].'" />';
                
                $row['level']=0;
                $row['cate_dir'] = $oModel->getData('cate_dir');
                $sLink = '<a href="?sMod=category_index&sAction=list_child&cat_id='.$row['product_category_id'].'&cat_name='.$row['product_category_name'].'&current_level='.$row['level'].'&cate_dir='.$row['cate_dir'].'">';
                if($row['cate_dir'] != '')
                    $row['cate_dir'].= " >> ";
                $row['cate_dir'].= $row['product_category_name'];
                $row['cate_dir'] = $row['cate_dir'];
                
                $this->oTemp->assign('rowCategory', $row);
                $this->oTemp->parse('main.rowCategory');
            }
        }else {
            $this->oTemp->parse('main.no_found');
        }
    //$this->oTemp->assign('navigator', $oModel->oNavigator->navigator());
    }
    public function displayListCategory($oModel) {
        $rs = $oModel->getListParentCategory();
        if (is_array($rs)) {
            $i = 0;
            foreach ($rs as $row) {
                $i++;
                $row['no'] = $i;
                $row['css'] = "row" . ($i % 2);
                if ($row['product_category_status'] == 1) {
                    $row['product_category_status'] = 'checked';
                }
                $this->oTemp->assign('rowCategory', $row);
                $this->oTemp->parse('main.rowCategory');
                $this->getAllChild($oModel,$row['product_category_id'],$level);
            }
        }
        else {
            $this->oTemp->parse('main.no_found');
        }
        $this->oTemp->assign('navigator',$oModel->oNavigator->navigator());
    }

    public function getAllChild($oModel,$parent_id,$level) {
        $rs=$oModel->getListChildOnly($parent_id);
        if(is_array($rs)) {
            $level++;
            foreach($rs as $row) {
                $row['product_category_name']=str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",$level).$row['product_category_name'];
                if ($row['product_category_status'] == 1) {
                    $row['product_category_status'] = 'checked';
                }
                $this->oTemp->assign('rowCategory', $row);
                $this->oTemp->parse('main.rowCategory');
                $this->getAllChild($oModel,$row[product_category_id],$level);
            }
        }
    }

    /**
     * DISPLAY CATEGORY LIKE OS COMMERCE()
     */
    public function displayListChildCategory($oModel) {
        $parent_id=$oModel->getData('parent_category_id');
        $rs=$oModel->getListChildOnly($parent_id);
        if(is_array($rs)) {
            foreach($rs as $row) {
                $current_level = $oModel->getData('current_level');
                $row['level'] = $current_level + 1;

                $i++;
                $row['no'] = $i;
                $row['css'] = "row" . ($i % 2);
                $rs_check_child=$oModel->getListChildOnly($row['product_category_id']);


                if(is_array($rs_check_child)) {
                    $row['prefix'] = "<img src='{URL_TEMPLATE_ADMIN}image/tree_img/folder.gif'/>";
                    $row['display_product_category_name']=
                        "<a href='?sMod=category_index&sAction=list_child&cat_id=".$row['product_category_id']."&cat_name=".$row['product_category_name']."&current_level=".$row['level']."'>".$row['product_category_name']."</a>";

                }
                else {
                    $row['prefix'] = "<img src='{URL_TEMPLATE_ADMIN}image/tree_img/page.gif'/>";
                    $row['display_product_category_name']="<a href='?sMod=category_index&sAction=edit_form&id=".$row['product_category_id']."'>".$row['product_category_name']."</a>";
                }


                if ($row['product_category_status'] == 1) {
                    $row['product_category_status'] = 'checked';
                }

                if ($row['product_category_statistics'] == 1) {
                    $row['product_category_statistics'] = 'checked';
                }

                if ($_SESSION['category_change_igm_id'] != '' && $_SESSION['category_change_igm_id'] ==
                    $row['product_category_id']) {

                    $category_image_old = $row['product_category_image'];
                    $oModel->deleteThumbImage($category_image_old);
                }
                $row['product_category_image_thumb'] = $oModel->getThumbImage($row['product_category_image'], 20, 20);
                $row['product_category_image_thumb'] = '<img src="'.$row['product_category_image_thumb'].'" border="0" alt="'.$row['product_category_name'].'" />';
                
                $row['cate_dir'] = $oModel->getData('cate_dir');
                $sLink = '<a href="?sMod=category_index&sAction=list_child&cat_id='.$row['product_category_id'].'&cat_name='.$row['product_category_name'].'&current_level='.$row['level'].'&cate_dir='.$row['cate_dir'].'">';
                if($row['cate_dir'] != '')
                    $row['cate_dir'].= " >> ";
                    
                $row['cate_dir'].= $row['product_category_name'];

                $this->oTemp->assign('rowCategory', $row);
                $this->oTemp->parse('main.rowCategory');
            }
        }
        else {
            $this->oTemp->parse('main.no_found');
        }
        //DISPLAY PATH CAT
        $level=$current_level;
        $rs_path_cat=$_SESSION['path_cat'];
        $count=$rs_path_cat;
        if(is_array($rs_path_cat)) {

            if($rs_path_cat[$level]['cat_id']!=$_GET['cat_id']) {
                $_SESSION['path_cat'][$level]=$_GET;
            }

            foreach($_SESSION['path_cat'] as $row_path_cat) {
                if($row_path_cat['current_level']>$_GET['current_level']) {
                    $_SESSION['path_cat'][$num_row]=null;
                }
                $num_row++;
            }
        }
        else {
            $_SESSION['path_cat'][$level]=$_GET;
        }
        //////////////////////////////////
        $rs_path_cat=$_SESSION['path_cat'];
        foreach($rs_path_cat as $row_path_cat) {
            if($row_path_cat!=null) {
                $row_path_cat['display_category_name']="<a href='?sMod=category_index&sAction=list_child&cat_id=".$row_path_cat['cat_id']."&cat_name=".$row_path_cat['cat_name']."&current_level=".$row_path_cat['current_level']."'>".$row_path_cat['cat_name']."</a>";
                $this->oTemp->assign('rowPathCat',$row_path_cat);
                $this->oTemp->parse('main.rowPathCat');
            }
        }
    }
    /**
     * ADD NEW CATEGORY
     */
    public function displayNewCategoryIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .'html/category_mamagement_new.html');
        $this->oTemp->assign('pageHeding', $oModel->aLanguage[MODULE_HEADING]['category']);
        $this->oTemp->assign('pathway_file', $oModel->getData('cate_dir') );
        $parent_category_id = $oModel->getData('parent_category_id');
        if($parent_category_id == ''){
            $row['parent_category_id'] = 0;
            $row['parent_category_name'] = $oModel->aLanguage['CATEGORY']['root'];
        }else{
            $row['parent_category_id'] = $oModel->getData('parent_category_id');
            $row['parent_category_name'] = $oModel->getData('parent_category_name');
        }
        $this->oTemp->assign('parent', $row);
        $this->oTemp->parse('main.parent');

        $this->oTemp->assign('parent_category_id', $row['parent_category_id']);
        $this->oTemp->assign('parent_category_name', $row['parent_category_name']);
        $this->oTemp->assign('current_level', $oModel->getData('current_level'));
        $this->oTemp->assign('cate_dir', base64_encode($oModel->getData('cate_dir') ) );
        /**
         * toolbar setup
         */
        $sCancel =  '?sMod=category_index';
        if($parent_category_id != '')
            $sCancel.= "&sAction=list_child&cat_id={$parent_category_id}&cat_name={$oModel->getData('parent_category_name')}&current_level={$oModel->getData('current_level')}&cate_dir={$oModel->getData('cate_dir')}";
        $this->oTemp->assign('product_category_status', 'checked');
        $this->oTemp->assign('toobar_save', 'check_cat_form()');
        $this->oTemp->parse('main.toobar_save');
        $this->oTemp->assign('toobar_cancel', $sCancel);
        $this->oTemp->parse('main.toobar_cancel');
    }
    
    public function displayBoxCategoryParent($oModel) {
        $rsSiteMap = $oModel->getListCategoryParentChild();
        if (is_array($rsSiteMap)) {
            $i = 0;
            foreach ($rsSiteMap as $rowSiteMap) {
                $i++;
                $rowSiteMap['no'] = $i;
                if ($rowSiteMap['product_category_id'] == $oModel->getData('product_category_parent_id')) {
                    $rowSiteMap['selected'] = 'selected';
                }
                $rowSiteMap['prefix'] = $this->displayTreePrefix($rowSiteMap['category_level']);
                $this->oTemp->assign('rowCatParent', $rowSiteMap);
                $this->oTemp->parse('main.rowCatParent');
            }
        }
    }
    private function displayTreePrefix($level) {
        $sPrefix = "";
        for ($i = 1; $i < $level; $i++) {
            $sPrefix .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|";
        }
        $sPrefix .= "&nbsp;";
        return $sPrefix;
    }
    private function displayTreePrefixImg($level) {
    //$sPrefix = "|";
        if ($level == 1)
            $sPrefix = "<img src='{URL_TEMPLATE_ADMIN}image/tree_img/folder.gif' alt=\"\" />";
        else {
            for ($i = 1; $i < $level; $i++) {
                $sPrefix .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            $sPrefix .= "<img src='{URL_TEMPLATE_ADMIN}image/tree_img/page.gif' alt=\"\" />&nbsp;";
        }
        return $sPrefix;
    }
    /**
     * EDIT CATEGORY
     */
    public function displayEditCategoryIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .
            'html/category_management_edit.html');
        $this->oTemp->assign_file('temp_editor', PATH_TEMPLATE_ADMIN .
            'html/temp_editor_basic.html');
        $this->oTemp->assign('pageHeding', $oModel->aLanguage[MODULE_HEADING]['category']);

        $this->oTemp->assign('pathway_file', $oModel->getData('cate_dir') );
        $parent_category_id = $oModel->getData('parent_category_id');
        if($parent_category_id == ''){
            $row['parent_category_id'] = 0;
            $row['parent_category_name'] = $oModel->aLanguage['CATEGORY']['root'];
        }else{
            $row['parent_category_id'] = $oModel->getData('parent_category_id');
            $row['parent_category_name'] = $oModel->getData('parent_category_name');
        }
        $this->oTemp->assign('parent', $row);
        $this->oTemp->parse('main.parent');
        $this->oTemp->assign('parent_category_id', $row['parent_category_id']);
        $this->oTemp->assign('parent_category_name', $row['parent_category_name']);
        $this->oTemp->assign('current_level', $oModel->getData('current_level'));
        $this->oTemp->assign('cate_dir', base64_encode($oModel->getData('cate_dir') ) );
        /**
         * toolbar setup
         */
          $sCancel =  '?sMod=category_index';
        if($parent_category_id != '')
            $sCancel.= "&sAction=list_child&cat_id={$parent_category_id}&cat_name={$oModel->getData('parent_category_name')}&current_level={$oModel->getData('current_level')}&cate_dir={$oModel->getData('cate_dir')}";
        $this->oTemp->assign('toobar_save', 'check_edit_cat_form()');
        $this->oTemp->parse('main.toobar_save');
        $this->oTemp->assign('toobar_cancel', $sCancel);
        $this->oTemp->parse('main.toobar_cancel');
    }

    public function displayInfoCat($rowManager,$oModel) {
        if ($rowManager['product_category_status'] == 1)
            $rowManager['product_category_status'] = "checked";
        $rowManager['product_category_yahoo_id_old'] = $rowManager['product_category_yahoo_id'];

        $rowParent = $oModel->getInfoCategory($rowManager['product_category_parent_id']);
        if(is_array($rowParent)) {
            $rowManager['product_category_parent_name']=$rowParent['product_category_name'];
        }
        else
            $rowManager['product_category_parent_name']=$oModel->aLanguage["CATEGORY"]['root'];

        $this->assignExtract($rowManager);
    }

    public function resultXML($oModel, $cCMD, $cResult) {
        $oJSON = new Services_JSON();
        echo $oJSON->encode(array('HEAD' => array('CMD' => $cCMD, 'DATA' => date('d/m/Y H:m:s'), ),
        'BODY' => array('RES' => $cResult)));
    }

    /*************************************************************************************************
    CATEGORY CHOOSE
    *************************************************************************************************/
    public function displatChooseCategoryIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .
            'html/category_choose.html');

        $this->oTemp->assign('pageHeding', $oModel->aLanguage['MENU']['choose_category']);

        /**
         * toolbar setup
         */

        $this->oTemp->assign('toobar_print', 'check_form()');
        $this->oTemp->parse('main.toobar_print');
        $this->oTemp->assign('toobar_cancel', '?sMod=category_index');
        $this->oTemp->parse('main.toobar_cancel');
    }


    public function ajax_displayListParentCategory($oModel) {

        $rs = $oModel->getListParentCategoryChoose();
        if (is_array($rs)) {
            $num_row = 0;
            foreach ($rs as $row) {
                $rs_child = $oModel->getListChildOnly($row['product_category_id']);
                if (is_array($rs_child)) {
                //$row['parent_img'] = URL_TEMPLATE_ADMIN . "image/arrow.gif";
                    $row['parent_img']='blue';
                }
                else
                //$row['parent_img'] =	URL_TEMPLATE_ADMIN . "image/blank.png";
                    $row['parent_img']='#999999';


                $row['highlight'] = "";
                $row['row_id'] = 0;
                $row['num_row'] = "parent" . $num_row;

                $this->oTemp->assign('i',$num_row%2);
                $num_row++;

                $this->oTemp->assign('RowCat', $row);
                $this->oTemp->parse('main.ResultCat.RowCat');
            }
            $this->oTemp->assign('row_id', 0);
            $this->oTemp->parse('main.ResultCat');
        }
        return $this->oTemp->text('main.ResultCat');
    }

    public function ajax_displayListChildCateogry($oModel, $parent_id) {
        $rs = $oModel->getListChildCategoryChoose($parent_id);
        $current_level = $oModel->getData('current_level');
        $next_level = $current_level + 1;
        //$_SESSION['test_count']=$next_level;

        if (is_array($rs)) {
            $num_row=0;
            foreach ($rs as $row) {
                $rs_child = $oModel->getListChildCategoryChoose($row['product_category_id']);

                if (is_array($rs_child)) {
                //$row['parent_img'] = URL_TEMPLATE_ADMIN . "image/arrow.gif";
                    $row['parent_img']='blue';
                }
                else
                //$row['parent_img'] =	URL_TEMPLATE_ADMIN . "image/blank.png";
                    $row['parent_img']='#999999';

                $row['num_row'] = $next_level."_child" . $num_row;
                $num_row++;

                $row['row_id'] = $next_level;

                $this->oTemp->assign('i',$num_row%2);
                $num_row++;

                $this->oTemp->assign('RowCat', $row);
                $this->oTemp->parse('main.ResultCat.RowCat');
            }
            $this->oTemp->assign('row_id', $next_level);
            $this->oTemp->parse('main.ResultCat');
        }
        $aResult[0]=$this->oTemp->text('main.ResultCat');
        $aResult[1]=$next_level;
        return $aResult;
    }
}
